<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :tree-node="company" :is-root="true" @addDepartments="addDepartments" />
        <!-- 以上是头部的公司信息，下面是树形的部门结构 -->
        <el-tree :expand-on-click-node="false" :data="departs" :props="{label: 'name'}" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <template #default="scoped">
            <treeTools :tree-node="scoped.data" :is-root="false" @addDepartments="addDepartments" @editDepartment="editDepartment" @delDepartment="getDepartments" />
          </template>
        </el-tree>
      </el-card>
      <Adddept
        ref="addDept"
        :tree-node="treeNode"
        :show-dialog.sync="showDialog"
        @addDepartments="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import Adddept from './components/add-dept'
import treeTools from './components/tree-tools'
import { listToTreeData } from '@/utils'
export default {
  components: {
    treeTools,
    Adddept
  },
  data() {
    return {
      // 弹窗默认为false
      showDialog: false,
      company: {
        name: '上海王俊凯影视文化工作室',
        mamsger: '负责人',
        id: ''
      },
      departs: [],
      treeNode: {},
      defaultProps: {
        // 表示 从这个属性显示内容
        lable: 'name'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 删除子部门
    // 这里的getDepartments是封装的方法
    async getDepartments() {
      // 这里的 getDepartments 是调用的api方法
      const res = await getDepartments()
      console.log(res)
      // this.departs = res.depts
      this.departs = listToTreeData(res.depts, '')
    },
    // 子传父，弹窗改为true
    addDepartments(treeNode) {
      this.showDialog = true
      this.treeNode = treeNode
    },
    // 子传父，弹窗改为true
    editDepartment(treeNode) {
      this.showDialog = true
      this.treeNode = treeNode
      this.$refs.addDept.getDepartmentDetail(treeNode.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
  font-weight: bold;
}
</style>
